import { RouteRecordRaw, createRouter, createWebHashHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { getNames } from "@/utils/handleRoutes";

import { useSettingStoreHook } from "@/store/setting";
const settingStore = useSettingStoreHook();
// export const loginRouter = {
//     path: "/login",
//     name: "LoginPage",
//     component: () => import("@/views/login/index.vue"),
//     meta: {
//         role: ["common", "admin"],
//     },
//     children: [],
// };

// export const homeRouter = {
//     path: "/",
//     name: "home",
//     component: () => import("../views/home/index.vue"),
//     meta: {},
//     children: [],
// };

export const aboutRouter = {
    path: "/about",
    name: "about",
    component: () => import("../views/about/index.vue"),
    meta: {},
    children: [],
} as RouteRecordRaw;

export const chatRouter = {
    path: "/chat",
    name: "chat",
    component: () => import("../views/chat/index.vue"),
    meta: {},
    children: [],
} as RouteRecordRaw;
export const bigScreenRouter = {
    path: "/bigScreen",
    name: "bigScreenPage",
    component: () => import("@/views/bigScreen/index.vue"),
    meta: {},
    children: [],
};

// 组合路由信息
// import.meta.glob 为 vite 提供的特殊导入方式
// 它可以将模块中全部内容导入并返回一个Record对象
// 默认为懒加载模式 加入配置项 eager 取消懒加载
const modules: Record<string, any> = import.meta.glob(["./modules/*.ts"], {
    eager: true,
});
// 配置路由
const routes: Array<RouteRecordRaw> = [];

// 将路由全部导入数组
Object.keys(modules).forEach((key) => {
    routes.push(modules[key].default);
});
routes.push(aboutRouter);
routes.push(chatRouter);
routes.push(bigScreenRouter);
// 配置路由
// const routes: Array<RouteRecordRaw> = [
//     {
//         path: '/',
//         name: 'home',
//         component: () => import('../views/home/index.vue'),
//         meta: {},
//         children: [],
//     },
//     {
//         path: '/login',
//         name: 'LoginPage',
//         component: () => import('@/views/login/index.vue'),
//         meta: {
//             role: ['common', 'admin'],
//         },
//         children: [],
//     },
// ];

export const handleBreadcrumb = (currentRouter: string) => {
    // console.log("handleBreadcrumb-- currentRouter", currentRouter);
    const names = getNames(currentRouter, router.getRoutes());
    settingStore.changeRouterName(names);
    // 获取所以的路由信息
    // console.log("router.getRoutes()", router.getRoutes());
};

//导入生成的路由数据
console.log(import.meta.env.VITE_ROUTER_BASE_URL);
const router = createRouter({
    // history: createWebHistory(import.meta.env.VITE_ROUTER_BASE_URL),
    history: createWebHashHistory(),
    routes,
});

const noStatusPage = ["/login", "/about", "chat"];
router.beforeEach(async (_to, _from, next) => {
    // debugger;
    NProgress.start();
    // const userStore = useUserStoreHook();
    // await userStore.refreshUserInfo();
    // const userIsLogin = userStore.isLogin();
    const token = sessionStorage.getItem("userInfo");
    const userIsLogin = token ? true : false;
    if (userIsLogin || noStatusPage.includes(_to.path)) {
        next();
    } else {
        next("/login");
    }
    // 处理面包屑的逻辑
    handleBreadcrumb(_to.name as string);
});
// router.beforeEach((to, from, next) => {
//     NProgress.start();
//     next();
// });
router.afterEach((to, from, next) => {
    NProgress.done();
});
export default router;
